﻿using Senparc.NeuChar.Entities;
using Senparc.Weixin.MP.Entities;
using Senparc.Weixin.MP.Entities.Request;
using Senparc.Weixin.MP.MessageHandlers;
using System.IO;
using System.Threading.Tasks;

namespace JNPF.Basics.Logic
{
    /// <summary>
    /// 自定义MP消息类
    /// </summary>
    public class CustomMessageHandler : MessageHandler<CustomMessageContext>
    {
        private MPEventContentBll mPEventContentBll = new MPEventContentBll();

        /// <summary>
        /// 构造函数（inputStream）
        /// </summary>
        /// <param name="inputStream">inputStream</param>
        /// <param name="postModel">postModel</param>
        /// <param name="maxRecordCount">单个用户上下文消息列表储存的最大长度</param>
        public CustomMessageHandler(Stream inputStream, PostModel postModel = null, int maxRecordCount = 0)
       : base(inputStream, postModel, maxRecordCount)
        {

        }
        /// <summary>
        /// 消息基类
        /// </summary>
        /// <param name="requestMessage">消息</param>
        /// <returns></returns>
        public override IResponseMessageBase DefaultResponseMessage(IRequestMessageBase requestMessage)
        {
            var responseMessage = base.CreateResponseMessage<ResponseMessageText>();
            responseMessage.Content = "这条消息来自DefaultResponseMessage。";
            return responseMessage;
        }
        /// <summary>
        /// 文本消息
        /// </summary>
        /// <param name="requestMessage">消息</param>
        /// <returns></returns>
        public override IResponseMessageBase OnTextRequest(RequestMessageText requestMessage)
        {
            var responseMessage = base.CreateResponseMessage<ResponseMessageText>();
            responseMessage.Content = "您的OpenID是：" + requestMessage.FromUserName + "。\r\n您发送了文字信息：" + requestMessage.Content;
            return responseMessage;
        }
        /// <summary>
        /// 图片类型请求
        /// </summary>
        /// <param name="requestMessage"></param>
        /// <returns></returns>
        public override IResponseMessageBase OnImageRequest(RequestMessageImage requestMessage)
        {
            var responseMessage = base.CreateResponseMessage<ResponseMessageImage>();
            responseMessage.Image.MediaId = requestMessage.MediaId;
            return responseMessage;
        }
        /// <summary>
        /// 文件请求
        /// </summary>
        /// <param name="requestMessage"></param>
        /// <returns></returns>
        public override IResponseMessageBase OnFileRequest(RequestMessageFile requestMessage)
        {
            var responseMessage = base.CreateResponseMessage<ResponseMessageText>();
            responseMessage.Content = "您的文件标题：" + requestMessage.Title + "。\r\n您文件描述：" + requestMessage.Description;
            return responseMessage;
        }
        /// <summary>
        /// 链接消息类型请求
        /// </summary>
        /// <param name="requestMessage"></param>
        /// <returns></returns>
        public override IResponseMessageBase OnLinkRequest(RequestMessageLink requestMessage)
        {
            var responseMessage = base.CreateResponseMessage<ResponseMessageText>();
            responseMessage.Content = "您的连接标题：" + requestMessage.Title + "。\r\n您连接描述：" + requestMessage.Description + "。\r\n您连接地址：" + requestMessage.Url;
            return responseMessage;
        }
        /// <summary>
        /// 位置类型请求
        /// </summary>
        /// <param name="requestMessage"></param>
        /// <returns></returns>
        public override IResponseMessageBase OnLocationRequest(RequestMessageLocation requestMessage)
        {
            var responseMessage = base.CreateResponseMessage<ResponseMessageText>();
            responseMessage.Content = "纬度：" + requestMessage.Location_X + "经度：" + requestMessage.Location_Y + "。\r\n位置：" + requestMessage.Label;
            return responseMessage;
        }
        /// <summary>
        /// 小视频类型请求
        /// </summary>
        /// <param name="requestMessage"></param>
        /// <returns></returns>
        public override IResponseMessageBase OnShortVideoRequest(RequestMessageShortVideo requestMessage)
        {
            var responseMessage = base.CreateResponseMessage<ResponseMessageText>();
            responseMessage.Content = "小视频缩略图：" + requestMessage.ThumbMediaId + "。\r\n描述：" + requestMessage.MediaId;
            return responseMessage;
        }
        /// <summary>
        /// 视频类型请求
        /// </summary>
        /// <param name="requestMessage"></param>
        /// <returns></returns>
        public override IResponseMessageBase OnVideoRequest(RequestMessageVideo requestMessage)
        {
            var responseMessage = base.CreateResponseMessage<ResponseMessageVideo>();
            responseMessage.Video.MediaId = requestMessage.MediaId;
            responseMessage.Video.Title = "标题";
            responseMessage.Video.Description = "视频反馈";
            return responseMessage;
        }
        /// <summary>
        /// 语音类型请求
        /// </summary>
        /// <param name="requestMessage"></param>
        /// <returns></returns>
        public override IResponseMessageBase OnVoiceRequest(RequestMessageVoice requestMessage)
        {
            var responseMessage = base.CreateResponseMessage<ResponseMessageVoice>();
            responseMessage.Voice.MediaId = requestMessage.MediaId;
            return responseMessage;
        }
        /// <summary>
        /// Event事件类型请求之CLICK
        /// </summary>
        /// <param name="requestMessage">事件之取消订阅</param>
        /// <returns></returns>
        public override async Task<IResponseMessageBase> OnEvent_ClickRequestAsync(RequestMessageEvent_Click requestMessage)
        {
            IResponseMessageBase responseMessage = null;
            switch (requestMessage.EventKey)
            {
                case "5162762d959d3b508bd629c012175b4a"://返回文本
                    {
                        var entity = await mPEventContentBll.GetInfo(requestMessage.EventKey);
                        var strongResponseMessage = base.CreateResponseMessage<ResponseMessageText>();
                        responseMessage = strongResponseMessage;
                        strongResponseMessage.Content = entity.Content;
                    }
                    break;
                case "64ba84f3178821b6e3c2a0889c68907c"://返回图文
                    {
                        var strongResponseMessage = base.CreateResponseMessage<ResponseMessageNews>();
                        responseMessage = strongResponseMessage;
                        strongResponseMessage.Articles.Add(new Article()
                        {
                            Title = "图文菜单按钮",
                            Description = "经典诗文选读，图文按钮测试",
                            PicUrl = "https://www.jnpfsoft.com:8090/api/Common/Img/20191016_74afae330003480295f18c3ff33d275e@jpeg",
                            Url = "https://mp.weixin.qq.com/s?__biz=MzUyNzYxOTk1Ng==&tempkey=MTAzMV9nSUR0NFFvZU51dzZZb0pucDFLekE1LUJEM3hSOThWeXNLejhPUXRZdlVrbnRoZEdvVTNtNWhGdThMTGN2SUlOM09IY2cyZ0o0cENOd3Z3ZU5CYXY2YVNwdVhUNzRGMUhEenhBWlZSMy01QkFSbEtCLUdtSFU5REd6LVNhSHY0NElXUFZTS250MkNEUDQ2TjN2NkhyZGV2ejU3Q0x5SFc4R2ZEa3VBfn4%3D&chksm=7a7d835f4d0a0a49ca5e00aec53cbfccea74ff1074d0c4d73c867e6d6e3ebc80754ae530371f#rd"
                        });
                    }
                    break;
                case "fc1534fc05b2bf7f9656625e6422052e"://返回音乐
                    {
                        //设置音乐信息
                        var strongResponseMessage = base.CreateResponseMessage<ResponseMessageMusic>();
                        responseMessage = strongResponseMessage;
                        strongResponseMessage.Music.Title = "音频测试";
                        strongResponseMessage.Music.Description = "熟悉的歌，最美的你";
                        strongResponseMessage.Music.MusicUrl = "https://www.jnpfsoft.com:8090/api/Common/IMVoice/3e5b12a4f16f426da8c7ea2631eccfea@mp3";
                        strongResponseMessage.Music.HQMusicUrl = "https://www.jnpfsoft.com:8090/api/Common/IMVoice/3e5b12a4f16f426da8c7ea2631eccfea@mp3";
                        strongResponseMessage.Music.ThumbMediaId = "IhnSgBOEHADAm564I70P9CN-qbowm3K8y7M3YNmwCqk";
                    }
                    break;
                case "fbc123c6f9b07e3e864bd8a128ae5b93"://返回图片
                    {
                        //设置图片信息
                        var strongResponseMessage = base.CreateResponseMessage<ResponseMessageImage>();
                        responseMessage = strongResponseMessage;
                        strongResponseMessage.Image.MediaId = "IhnSgBOEHADAm564I70P9IZfd5mA4naswJflRwXRijk";
                    }
                    break;
                default:
                    {
                        var strongResponseMessage = base.CreateResponseMessage<ResponseMessageText>();
                        strongResponseMessage.Content = "您的OpenID是：" + requestMessage.FromUserName + "。\r\n您发送了文字信息：" + requestMessage.EventKey;
                        responseMessage = strongResponseMessage;
                    }
                    break;
            }
            return responseMessage;
        }
        /// <summary>
        /// 事件之扫码推事件(scancode_push)
        /// </summary>
        /// <param name="requestMessage"></param>
        /// <returns></returns>
        public override IResponseMessageBase OnEvent_ScancodePushRequest(RequestMessageEvent_Scancode_Push requestMessage)
        {
            var responseMessage = base.CreateResponseMessage<ResponseMessageText>();
            responseMessage.Content = "事件之扫码推事件";
            return responseMessage;
        }
        /// <summary>
        /// 事件之扫码推事件且弹出“消息接收中”提示框(scancode_waitmsg)
        /// </summary>
        /// <param name="requestMessage"></param>
        /// <returns></returns>
        public override IResponseMessageBase OnEvent_ScancodeWaitmsgRequest(RequestMessageEvent_Scancode_Waitmsg requestMessage)
        {
            var responseMessage = base.CreateResponseMessage<ResponseMessageText>();
            responseMessage.Content = "事件之扫码推事件且弹出“消息接收中”提示框";
            return responseMessage;
        }
        /// <summary>
        /// 事件之弹出系统拍照发图(pic_sysphoto)
        /// 实际测试时发现微信并没有推送RequestMessageEvent_Pic_Sysphoto消息，只能接收到用户在微信中发送的图片消息。
        /// </summary>
        /// <param name="requestMessage"></param>
        /// <returns></returns>
        public override IResponseMessageBase OnEvent_PicSysphotoRequest(RequestMessageEvent_Pic_Sysphoto requestMessage)
        {
            var responseMessage = base.CreateResponseMessage<ResponseMessageText>();
            responseMessage.Content = "事件之弹出系统拍照发图";
            return responseMessage;
        }
        /// <summary>
        /// 事件之弹出拍照或者相册发图（pic_photo_or_album）
        /// </summary>
        /// <param name="requestMessage"></param>
        /// <returns></returns>
        public override IResponseMessageBase OnEvent_PicPhotoOrAlbumRequest(RequestMessageEvent_Pic_Photo_Or_Album requestMessage)
        {
            var responseMessage = base.CreateResponseMessage<ResponseMessageText>();
            responseMessage.Content = "事件之弹出拍照或者相册发图";
            return responseMessage;
        }
        /// <summary>
        /// 事件之弹出微信相册发图器(pic_weixin)
        /// </summary>
        /// <param name="requestMessage"></param>
        /// <returns></returns>
        public override IResponseMessageBase OnEvent_PicWeixinRequest(RequestMessageEvent_Pic_Weixin requestMessage)
        {
            var responseMessage = base.CreateResponseMessage<ResponseMessageText>();
            responseMessage.Content = "事件之弹出微信相册发图器";
            return responseMessage;
        }
        /// <summary>
        /// 事件之弹出地理位置选择器（location_select）
        /// </summary>
        /// <param name="requestMessage"></param>
        /// <returns></returns>
        public override IResponseMessageBase OnEvent_LocationSelectRequest(RequestMessageEvent_Location_Select requestMessage)
        {
            var responseMessage = base.CreateResponseMessage<ResponseMessageText>();
            responseMessage.Content = "事件之弹出地理位置选择器";
            return responseMessage;
        }
    }
}
